/**
 * CopyRight © 2012 USTC SINOVATE SOFTWARE CO.LTD All Rights Reserved.
 */

package com.cmcc.zysoft.infocollection.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.cmcc.zysoft.infocollection.common.BaseController;
import com.cmcc.zysoft.infocollection.model.Channel;
import com.cmcc.zysoft.infocollection.model.Industry;
import com.cmcc.zysoft.infocollection.service.ChannelPcService;
import com.cmcc.zysoft.infocollection.service.IndustryPcService;

/**
 * @author 袁凤建
 * <br />邮箱: yuan.fengjian@ustcinfo.com
 * <br />描述: IndustryController.java
 * <br />版本: 1.0.0
 * <br />日期: 2013-8-14 下午4:01:58
 * <br />CopyRight © 2012 USTC SINOVATE SOFTWARE CO.LTD All Rights Reserved.
 */

@Controller
@RequestMapping("/pc/industry")
public class IndustryController extends BaseController {
	
	/**
	 * 属性名称: channelPcService.
	 * 类       型: ChannelPcService.
	 */
	@Resource
	private ChannelPcService channelPcService;

	/**
	 * 属性名称: industryPcService.
	 * 类       型: IndustryPcService.
	 */
	@Resource
	private IndustryPcService industryPcService;
	
	/**
	 * 行业管理页面.
	 * @return String
	 */
	@RequestMapping("/main.htm")
	public String main() {
		return "/pc/industry/list";
	}

	/**
	 * 获取行业分页列表信息.
	 * @param page 页码
	 * @param rows 每页记录数
	 * @param keyword 搜索关键字
	 * @return Map<String, Object>
	 */
	@RequestMapping("/list.htm")
	@ResponseBody
	public Map<String, Object> getIndustryList(int page, int rows, String keyword) {
		return this.industryPcService.getIndustryList(page, rows, keyword);
	}
	
	/**
	 * 添加行业页面.
	 * @param modelMap modelMap
	 * @return String
	 */
	@RequestMapping("/add.htm")
	public String add(ModelMap modelMap) {
		List<Map<String, Object>> channelList = this.channelPcService.getChannelList();
		modelMap.addAttribute("channelList", channelList);
		return "/pc/industry/add";
	}

	/**
	 * 保存/更新行业信息.
	 * @param industry 行业信息
	 * @param channelId 频道ID
	 * @return String
	 */
	@RequestMapping("/saveIndustry.htm")
	@ResponseBody
	@Transactional
	public String addIndustry(Industry industry, String channelId) {
		Channel channel = this.channelPcService.getEntity(channelId);
		industry.setChannel(channel);
		if(StringUtils.hasText(industry.getIndustryId())) {
			this.industryPcService.updateEntity(industry);
		} else {
			this.industryPcService.insertEntity(industry);
		}
		return "SUCCESS";
	}
	
	
	/**
	 * 修改行业信息页面.
	 * @param modelMap modelMap
	 * @param industryId 行业ID
	 * @return String
	 */
	@RequestMapping("/edit.htm")
	public String edit(ModelMap modelMap, String industryId) {
		List<Map<String, Object>> channelList = this.channelPcService.getChannelList();
		Industry industry = this.industryPcService.getEntity(industryId);
		modelMap.addAttribute("channelList", channelList);
		modelMap.addAttribute("industry", industry);
		modelMap.addAttribute("channelId", industry.getChannel().getChannelId());
		return "/pc/industry/edit";
	}
	
	/**
	 * 删除行业.
	 * @param industryIds 行业ID集合
	 * @return String
	 */
	@RequestMapping("/remove.htm")
	@ResponseBody
	@Transactional
	public String remove(String industryIds) {
		String[] ids = industryIds.split(",");
		for(int i = 0; i < ids.length; i++) {
			this.industryPcService.deleteEntity(ids[i]);
		}
		return "SUCCESS";
	}
	
	/**
	 * 检查行业名称是否已经存在.
	 * @param industryName 行业名称
	 * @param industryId 行业ID
	 * @return map
	 */
	@RequestMapping("/check.htm")
	@ResponseBody
	public Map<String, Object> check(String industryName, String industryId) {
		Map<String, Object> map = new HashMap<String, Object>();
		Boolean isExist = this.industryPcService.check(industryName, industryId);
		if(isExist) {
			map.put("code", "0");
		} else {
			map.put("code", "1");
		}
		return map;
	}
	
	/**
	 * 根据频道ID查找行业列表信息.
	 * @param channelId 频道ID
	 * @return List<Map<String, Object>>
	 */
	@RequestMapping("/industrys.htm")
	@ResponseBody
	public List<Map<String, Object>> industrys(String channelId) {
		return this.industryPcService.industrys(channelId);
	}
}